package com.depth.viewer.widget
{
	import com.depth.viewer.container.ViewerContainer;
	import com.depth.viewer.events.AppEvent;
	import com.depth.viewer.manager.ViewerSystemManager;
	
	import flash.events.MouseEvent;
	
	public class BaseFocusWidget extends BaseWidget implements IFocusWidget,IBaseMediatorWidget
	{
		public function BaseFocusWidget()
		{
			super();
			addEventListener(MouseEvent.MOUSE_DOWN,mouse_downHandler);
			
			ViewerContainer.addEventListener(AppEvent.WIDGET_MINIMIZED,widgetMinimizedHandler);
			ViewerContainer.addEventListener(AppEvent.WIDGET_OPEN_START,widgetOpenedHandler);
			
		}
		private function widgetMinimizedHandler(event:AppEvent):void{
			if(event.data == this)
				this.visible = false;
		}
		private function widgetOpenedHandler(event:AppEvent):void{
			if(event.data == this)
				this.visible = true;
		}
		protected function mouse_downHandler(event:MouseEvent):void{
			var baseWidget:BaseWidget = this;
			if(baseWidget is IFocusWidget){
				IFocusWidget(baseWidget).focusIn();
			}
		}
		
		private var _focalize:Boolean = false;
		public function focusIn():void{
			var widgets:Array = ViewerSystemManager.getInstance().widgetManager.getRegistry().getRegistry().getRegistedWidgets();
			for each(var widget:IBaseWidget in widgets){
				if(widget is IFocusWidget && widget.widgetId != widgetId){
					IFocusWidget(widget).focusOut();
				}
			}
			container.focusWidget(widgetId);
			this._focalize = true;
			this.alpha = 1;
			ViewerContainer.dispatchEvent(new AppEvent(AppEvent.WIDGET_FOCUS_IN,this));
		}
		public function focusOut():void{
			this._focalize = false;
			this.alpha = 1;
			ViewerContainer.dispatchEvent(new AppEvent(AppEvent.WIDGET_FOCUS_OUT,this));
		}
		public function get focalize():Boolean{
			return this._focalize;
		}
	}
}